#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
CRM旅游管理系统 Web应用启动入口

启动FastAPI Web服务器
"""

import uvicorn
import logging
import sys
from pathlib import Path

# 添加项目根目录到Python路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))

from src.core.config import get_settings

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(sys.stdout),
        logging.FileHandler('logs/crm.log', encoding='utf-8')
    ]
)

logger = logging.getLogger(__name__)


def main():
    """主函数"""
    settings = get_settings()

    logger.info("启动CRM Web应用...")
    logger.info("访问地址: http://" + settings.HOST + ":" + str(settings.PORT))
    logger.info("API文档: http://" + settings.HOST + ":" + str(settings.PORT) + "/docs")

    uvicorn.run(
        "src.app:app",
        host=settings.HOST,
        port=settings.PORT,
        reload=settings.DEBUG,
        log_level="info" if not settings.DEBUG else "debug",
        access_log=True
    )


if __name__ == "__main__":
    main()
